package com.yanboo.generator.rest;

import com.yanboo.common.annotation.CustomController;
import com.yanboo.logging.annotation.Log;
import com.yanboo.generator.domain.DbDriver;
import com.yanboo.generator.service.DbDriverService;
import com.yanboo.generator.service.dto.DbDriverQueryCriteria;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.data.domain.Pageable;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import java.io.IOException;
import java.util.List;

import jakarta.servlet.http.HttpServletResponse;
import com.yanboo.common.utils.PageResult;
import com.yanboo.generator.service.dto.DbDriverDto;

/**
 * @author fdq
 * @date 2023-12-01
 **/
@CustomController
@RequiredArgsConstructor
@Tag(name = "驱动管理管理")
@RequestMapping("/api/dbDriver")
public class DbDriverController {

    private final DbDriverService dbDriverService;

    @Log("导出数据")
    @Operation(summary = "导出数据")
    @GetMapping(value = "/download")
    public void exportDbDriver(HttpServletResponse response, DbDriverQueryCriteria criteria) throws IOException {
        dbDriverService.download(dbDriverService.queryAll(criteria), response);
    }

    @GetMapping
    @Log("查询驱动管理")
    @Operation(summary = "查询驱动管理")
    public ResponseEntity<PageResult<DbDriverDto>> queryDbDriver(DbDriverQueryCriteria criteria, Pageable pageable){
        return new ResponseEntity<>(dbDriverService.queryAll(criteria,pageable),HttpStatus.OK);
    }

    @GetMapping("/all")
    @Log("查询驱动管理")
    @Operation(summary = "查询驱动管理")
    public ResponseEntity<List<DbDriverDto>> queryDbDriver(){
        return new ResponseEntity<>(dbDriverService.queryAll(),HttpStatus.OK);
    }

    @PostMapping
    @Log("新增驱动管理")
    @Operation(summary = "新增驱动管理")
    public ResponseEntity<Object> createDbDriver(@Validated @RequestBody DbDriver resources){
        dbDriverService.create(resources);
        return new ResponseEntity<>(HttpStatus.CREATED);
    }

    @PutMapping
    @Log("修改驱动管理")
    @Operation(summary = "修改驱动管理")
    public ResponseEntity<Object> updateDbDriver(@Validated @RequestBody DbDriver resources){
        dbDriverService.update(resources);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    @DeleteMapping
    @Log("删除驱动管理")
    @Operation(summary = "删除驱动管理")
    public ResponseEntity<Object> deleteDbDriver(@RequestBody Long[] ids) {
        dbDriverService.deleteAll(ids);
        return new ResponseEntity<>(HttpStatus.OK);
    }
}